// UserRepository.java
package com.example.app3_1;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import java.util.List;
import java.io.IOException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserRepository {
    private ApiService apiService;
    private static final String API_KEY = "reqres-free-v1";

    public UserRepository() {
        apiService = RetrofitClient.getApiService();
    }

    public void getUsers(MutableLiveData<List<User>> userLiveData, MutableLiveData<String> errorMessage) {
        // 使用带认证的调用
        Call<UserListResponse> call = apiService.getUsers();

        call.enqueue(new Callback<UserListResponse>() {
            @Override
            public void onResponse(Call<UserListResponse> call, Response<UserListResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<User> users = response.body().getData();
                    if (users != null && !users.isEmpty()) {
                        userLiveData.setValue(users);
                        Log.d("UserRepository", "成功获取 " + users.size() + " 个用户");
                    } else {
                        errorMessage.setValue("暂无用户数据");
                        Log.d("UserRepository", "服务器返回空数据");
                    }
                } else {
                    // 更详细的错误处理
                    String errorMsg;
                    int statusCode = response.code();

                    switch (statusCode) {
                        case 400:
                            errorMsg = "请求参数错误";
                            break;
                        case 401:
                            errorMsg = "未授权访问 - 请检查 API Key 配置";
                            break;
                        case 403:
                            errorMsg = "访问被拒绝 - API Key 可能无效";
                            break;
                        case 404:
                            errorMsg = "接口不存在";
                            break;
                        case 429:
                            errorMsg = "请求过于频繁，请稍后重试";
                            break;
                        case 500:
                            errorMsg = "服务器内部错误";
                            break;
                        default:
                            errorMsg = "请求失败，错误码: " + statusCode;
                            break;
                    }

                    // 尝试获取服务器返回的错误信息
                    try {
                        if (response.errorBody() != null) {
                            String errorBody = response.errorBody().string();
                            Log.e("UserRepository", "错误响应: " + errorBody);
                        }
                    } catch (IOException e) {
                        Log.e("UserRepository", "读取错误响应失败", e);
                    }

                    errorMessage.setValue(errorMsg);
                    Log.e("UserRepository", "API请求失败: " + statusCode + " - " + errorMsg);
                }
            }

            @Override
            public void onFailure(Call<UserListResponse> call, Throwable t) {
                String errorMsg;
                if (t instanceof IOException) {
                    errorMsg = "网络连接失败，请检查网络设置";
                } else {
                    errorMsg = "请求失败: " + t.getMessage();
                }

                errorMessage.setValue(errorMsg);
                Log.e("UserRepository", "网络请求失败", t);
            }
        });
    }
}